package huanyuxingcheng.nonameinstallhelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ExtensionActivity extends BaseActivity implements View.OnClickListener {

    private Spinner createdSpinner;
    private Spinner downloadedSpinner;
    /**
     * 安装
     */
    private Button installButton;

    private ArrayList<File> dirs = new ArrayList<>();

    private ArrayList<File> files = new ArrayList<>();

    private BaseAdapter dirAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return dirs.size();
        }

        @Override
        public Object getItem(int i) {
            return dirs.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view == null){
                view = View.inflate(ExtensionActivity.this,R.layout.spinner_text_item,null);
            }
            TextView textView = view.findViewById(R.id.item_text);
            File file = dirs.get(i);
            textView.setText(file.getName());
            return view;
        }
    };

    private BaseAdapter fileAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return files.size();
        }

        @Override
        public Object getItem(int i) {
            return files.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view == null){
                view = View.inflate(ExtensionActivity.this,R.layout.spinner_text_item,null);
            }
            TextView textView = view.findViewById(R.id.item_text);
            File file = files.get(i);
            textView.setText(file.getName());
            return view;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_extension);
        initView();
        initSpinner();
    }

    private void initSpinner(){
        dirs.addAll(NonameUtils.getSubDirectories(new File(NonameUtils.getNonameDirectory(),"extension")));
        Collections.sort(dirs, new Comparator<File>() {
            @Override
            public int compare(File file, File t1) {
                if(file.getName().contains("玄武江湖")){
                    return -1;
                }
                if(t1.getName().contains("玄武江湖")){
                    return 1;
                }
                return t1.getName().compareTo(file.getName());
            }
        });
        createdSpinner.setAdapter(dirAdapter);
        createdSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                File dir = dirs.get(i);
                files.clear();
                files.addAll(NonameUtils.fileFilter(NonameUtils.getQQDownloadDirectory(),dir.getName(),".zip"));
                fileAdapter.notifyDataSetChanged();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                files.clear();
                fileAdapter.notifyDataSetChanged();
            }
        });
        downloadedSpinner.setAdapter(fileAdapter);
    }

    private void initView() {
        createdSpinner = findViewById(R.id.created_spinner);
        downloadedSpinner = findViewById(R.id.downloaded_spinner);
        installButton = findViewById(R.id.install_button);
        installButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.install_button:
                File dir = getSelectedDir();
                File file = getSelectedFile();
                if(dir != null && file != null){
                    Intent intent = new Intent(this,ZipActivity.class);
                    intent.putExtra("fromPath",file.getAbsolutePath());
                    intent.putExtra("toPath",dir.getAbsolutePath());
                    startActivity(intent);
                }else{
                    showShortToast("请选择文件");
                }
                break;
        }
    }

    private File getSelectedDir(){
        int pos = createdSpinner.getSelectedItemPosition();
        if(pos >=0 && pos < dirs.size()){
            return dirs.get(pos);
        }
        return null;
    }

    private File getSelectedFile(){
        int pos = downloadedSpinner.getSelectedItemPosition();
        if(pos >=0 && pos < files.size()){
            return files.get(pos);
        }
        return null;
    }
}
